package com.test.Task;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.dom4j.DocumentException;
import org.json.*;
import org.junit.Test;
import org.mortbay.util.ajax.JSON;

import com.pwc.conversionTool.IO.Reader;
import com.pwc.conversionTool.Manager.SoapUIManager;
import com.pwc.conversionTool.Manager.WsdlXmlConfigImpl;
import com.pwc.conversionTool.task.TaskManage;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class TeskTask {

	@Test
	public void testRunTask() throws InterruptedException{
//		TaskManage.getInstance().startTask(null, null, null, null);
//		TaskManage.getInstance().startTask(null,null,null);
		
		Thread.sleep(1000);
	}
	
	
//	@Test
	public void testXML() throws IOException, DocumentException{
		SoapUIManager.settingEndpoint("https://wd2-impl-services1.workday.com/ccx/service/pwc_dpt1/Staffing/v25.0?wsdl");
		
		String content=Reader.getStringFromFile("C:\\Users\\dwu102\\Desktop\\workday\\relation\\getPositions.json");
		
		JSONArray jsonarray=JSONArray.fromObject(content);
		JSONObject jsonObject=jsonarray.getJSONObject(0);
		
		Map result=new HashedMap();
		String key=null;
		String value=null;

		System.out.println("json:"+jsonObject.getString("csvtemplate"));
		String xmlString=SoapUIManager.getSoapUIClient().getTemplate(jsonObject.getString("csvtemplate"));
		WsdlXmlConfigImpl wsdlXmlConfigImpl=WsdlXmlConfigImpl.InitialString(xmlString);
		
		JSONArray jsonarray_mappingData=JSONArray.fromObject(jsonObject.get("mappingData"));
		JSONObject jsonObject_mappingData=jsonarray_mappingData.getJSONObject(0);
		Iterator iterator=jsonObject_mappingData.keys();
		while (iterator.hasNext()) {
            key = (String) iterator.next();
            value = jsonObject_mappingData.getString(key);
            wsdlXmlConfigImpl.setSingleStringValue(key,value);
             
            System.out.println(key);
            System.out.println(value);
            result.put(key, value);
        }
		
		System.out.println(wsdlXmlConfigImpl.getXMLString());
	}
	
//	@Test
	public void deleteXml() throws DocumentException, IOException{
		SoapUIManager.settingEndpoint("https://wd2-impl-services1.workday.com/ccx/service/pwc_dpt1/Staffing/v25.0?wsdl");
		
		String content=Reader.getStringFromFile("C:\\Users\\dwu102\\Desktop\\workday\\relation\\getPositions.json");
		
		JSONArray jsonarray=JSONArray.fromObject(content);
		JSONObject jsonObject=jsonarray.getJSONObject(0);
		
		Map result=new HashedMap();
		String key=null;
		String value=null;

		System.out.println("json:"+jsonObject.getString("csvtemplate"));
		String xmlString=SoapUIManager.getSoapUIClient().getTemplate(jsonObject.getString("csvtemplate"));
		WsdlXmlConfigImpl wsdlXmlConfigImpl=WsdlXmlConfigImpl.InitialString(xmlString);
		
		JSONArray jsonarray_deleteData=JSONArray.fromObject(jsonObject.get("deleteData"));
		JSONObject jsonObject_deleteData=jsonarray_deleteData.getJSONObject(0);
		Iterator iterator=jsonObject_deleteData.keys();
		while (iterator.hasNext()) {
            key = (String) iterator.next();
            value = jsonObject_deleteData.getString(key);
            wsdlXmlConfigImpl.deleteNodeByXpath(value);
             
            System.out.println("key:"+key);
            System.out.println("value:"+value);
            result.put(key, value);
        }
		System.out.println(wsdlXmlConfigImpl.getXMLString());
	}
	
//	@Test
	public void testFile() throws IOException{
		String string=Reader.getStringFromFile("C:\\Users\\dwu102\\Desktop\\workday\\relation\\file.txt");
		JSONArray jsonarray=JSONArray.fromObject(string);
		JSONObject jsonObject=jsonarray.getJSONObject(0);
		Iterator iterator=jsonObject.keys();
		String key=null;
		String value=null;

		while (iterator.hasNext()) {
            key = (String) iterator.next();
            value = jsonObject.getString(key);
             
            System.out.println(key);
            System.out.println(value);
        }
		
	}
	
	
	
}
